﻿using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;

namespace AgFractal.Controls
{
    public class PopupUserControl : UserControl
    {
        TranslateTransform move = new TranslateTransform();
        public PopupUserControl()
        {
            this.Loaded += PopupUserControl_Loaded;
            this.RenderTransform = move;
            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(PopupUserControl_MouseLeftButtonDown);
            this.MouseMove += new System.Windows.Input.MouseEventHandler(PopupUserControl_MouseMove);
            this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(PopupUserControl_MouseLeftButtonUp);
        }

        double Xmove;
        double Ymove;
        void PopupUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var popup = this.GetParent<Popup>();
            if (popup == null)
                return;
            var rootElement = (FrameworkElement)Application.Current.RootVisual;
            Xmove = (rootElement.ActualWidth + popup.ActualWidth) / 2 - 20;
            Ymove = (rootElement.ActualHeight + popup.ActualHeight) / 2 - 20;
        }

        void PopupUserControl_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            dragging = false;
            ReleaseMouseCapture();
            Cursor = oldCursor;
        }

        bool dragging;
        double startDragX;
        double startDragY;
        void PopupUserControl_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!dragging)
                return;
            var pos = e.GetPosition(null);

            double newX = startDragX + pos.X - startpos.X;
            if (newX < Xmove && -newX < Xmove)
                move.X = newX;

            double newY = startDragY + pos.Y - startpos.Y;
            if (newY < Ymove && -newY < Ymove)
                move.Y = newY;
        }

        Cursor oldCursor;
        Point startpos;
        void PopupUserControl_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!(e.OriginalSource is Panel))
                return;

            dragging = true;
            startpos = e.GetPosition(null);
            startDragX = move.X;
            startDragY = move.Y;
            CaptureMouse();
            oldCursor = Cursor;
            Cursor = Cursors.Hand;
        }

        internal Action<bool> OnClose;
        protected void Close(bool result)
        {
            var p = this.GetParent<Popup>();

            if (p != null)
            {
                p.IsOpen = false;
                var uc = p.GetParent<UserControl>();
                uc.IsEnabled = true;
            }

            if (OnClose != null)
                OnClose(result);
        }
    }
}